package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long parentId) {

        //根据父类别的id查询子类别列表
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId,parentId);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);
        //判断categoryList是否有子节点，并将结果填充到hasChildren属性中
        if(!CollectionUtils.isEmpty(categoryList)){
            categoryList.forEach(category -> {
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>().eq(Category::getParentId, category.getId()));
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //查询parent_id=0的所有Category列表
        List<Category> categoryList = baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        //将categoryList转换为CategoryVoList
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return categoryVoList;
    }

    @Override
    public List<CategoryVo> tree() {

        //将所有类别数据一次性查询出来
        List<Category> allCategoryList = baseMapper.selectList(null);

        //将List<Category>转换为List<CategoryVo>
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());

        //填充children
        List<CategoryVo> tree = buildTree(categoryVoList);
        return tree;

        //获取以及类别列表（db） 1
        //遍历一级类别列表{
            //根据一级类别列表id查找二级类别列表（db） 100
            //遍历二级类别列表{
                //根据二级类别id查找三级类别列表(db) 100
            //}
        // }

    }

    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {

        ArrayList<CategoryVo> tree = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            //判断是否是一级节点
            if(categoryVo.getParentId().longValue() == 0){
                //找到子节点,将子节点填充到children属性中
                categoryVo = findAndAddChildren(categoryVo,categoryVoList);
                //组装tree节点
                tree.add(categoryVo);
            }
        }

        return tree;
    }

    /**
     * 为categoryVo填充子节点，子节点在categoryVoList查询
     * @param categoryVo
     * @param categoryVoList
     * @return
     */
    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历categoryVoList节点找到categoryVo的children
        for (CategoryVo vo : categoryVoList) {
            //判断当前vo是否是categoryVo的子节点
            if(vo.getParentId().longValue() == categoryVo.getId().longValue()){
                vo = findAndAddChildren(vo, categoryVoList);
                categoryVo.getChildren().add(vo);
            }
        }
        return categoryVo;
    }
}
